import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: "/user-manage",
    component: Layouts,
    redirect: "/user-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/user-manage/index.vue"),
        name: "UserManage",
        meta: {
          title: "用户管理",
          svgIcon: "user-manage"
        }
      }
    ]
  },
  {
    path: "/merchant-manage",
    component: Layouts,
    redirect: "/merchant-manage/apply",
    name: "MerchantManage",
    meta: {
      title: "商户管理",
      svgIcon: "shop"
    },
    children: [
      {
        path: "apply",
        // component: () => import("@/views/merchant-manage/apply/index.vue"),
        component: () => import("@/views/merchant-manage/apply/index.vue"),
        name: "MerchantApply",
        meta: {
          title: "商户入驻审核",
          keepAlive: true
        }
      },
      {
        path: "info",
        component: () => import("@/views/merchant-manage/info/index.vue"),
        name: "MerchantInfo",
        meta: {
          title: "商户信息管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/delivery-manage",
    component: Layouts,
    redirect: "/delivery-manage/apply",
    name: "DeliveryManage",
    meta: {
      title: "配送员管理",
      svgIcon: "delivery"
    },
    children: [
      {
        path: "apply",
        component: () => import("@/views/delivery-manage/apply/index.vue"),
        name: "DeliveryApply",
        meta: {
          title: "配送员认证审核",
          keepAlive: true
        }
      },
      {
        path: "info",
        component: () => import("@/views/delivery-manage/info/index.vue"),
        name: "DeliveryInfo",
        meta: {
          title: "配送员信息管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/order-manage",
    component: Layouts,
    redirect: "/order-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/order-manage/index.vue"),
        name: "OrderManage",
        meta: {
          title: "订单管理",
          svgIcon: "order-manage"
        }
      }
    ]
  },
  {
    path: "/coupon-manage",
    component: Layouts,
    redirect: "/coupon-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/coupon-manage/index.vue"),
        name: "CouponManage",
        meta: {
          title: "优惠券管理",
          svgIcon: "coupon"
        }
      }
    ]
  },
  {
    path: "/banner-manage",
    component: Layouts,
    redirect: "/banner-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/banner-manage/index.vue"),
        name: "BannerManage",
        meta: {
          title: "轮播图管理",
          svgIcon: "banner"
        }
      }
    ]
  },
  {
    path: "/notice-manage",
    component: Layouts,
    redirect: "/notice-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/notice-manage/index.vue"),
        name: "NoticeManage",
        meta: {
          title: "公告管理",
          svgIcon: "notice"
        }
      }
    ]
  },
  {
    path: "/canteen-manage",
    component: Layouts,
    redirect: "/canteen-manage/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/canteen-manage/index.vue"),
        name: "CanteenManage",
        meta: {
          title: "食堂管理",
          svgIcon: "canteen"
        }
      }
    ]
  },
  {
    path: "/points-record",
    component: Layouts,
    redirect: "/points-record/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/points-record/index.vue"),
        name: "PointsRecord",
        meta: {
          title: "积分记录",
          svgIcon: "points"
        }
      }
    ]
  },
  {
    path: "/feedback-manage",
    component: Layouts,
    redirect: "/feedback-manage/index",
    name: "FeedbackManage",
    meta: {
      title: "反馈管理",
      svgIcon: "feedback"
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/feedback-manage/index.vue"),
        name: "FeedbackList",
        meta: {
          title: "意见反馈",
          keepAlive: true
        }
      },
      {
        path: "feedback-type",
        component: () => import("@/views/feedback-manage/feedback-type.vue"),
        name: "FeedbackType",
        meta: {
          title: "反馈类型管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/statistics",
    component: Layouts,
    redirect: "/statistics/order",
    name: "Statistics",
    meta: {
      title: "数据统计",
      svgIcon: "chart"
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/statistics/order/index.vue"),
        name: "OrderStatistics",
        meta: {
          title: "订单统计",
          keepAlive: true
        }
      },
      {
        path: "merchant-revenue",
        component: () => import("@/views/statistics/merchant-revenue/index.vue"),
        name: "MerchantRevenue",
        meta: {
          title: "商户营收分析",
          keepAlive: true
        }
      },
      {
        path: "platform-revenue",
        component: () => import("@/views/statistics/platform-revenue/index.vue"),
        name: "PlatformRevenue",
        meta: {
          title: "平台营收分析",
          keepAlive: true
        }
      }
    ]
  },
  // {
  //   path: "/unocss",
  //   component: Layouts,
  //   redirect: "/unocss/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/unocss/index.vue"),
  //       name: "UnoCSS",
  //       meta: {
  //         title: "UnoCSS",
  //         svgIcon: "unocss"
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/link",
  //   meta: {
  //     title: "外链",
  //     svgIcon: "link"
  //   },
  //   children: [
  //     {
  //       path: "https://juejin.cn/post/7089377403717287972",
  //       component: () => {},
  //       name: "Link1",
  //       meta: {
  //         title: "中文文档"
  //       }
  //     },
  //     {
  //       path: "https://juejin.cn/column/7207659644487139387",
  //       component: () => {},
  //       name: "Link2",
  //       meta: {
  //         title: "新手教程"
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/table",
  //   component: Layouts,
  //   redirect: "/table/element-plus",
  //   name: "Table",
  //   meta: {
  //     title: "表格",
  //     elIcon: "Grid"
  //   },
  //   children: [
  //     {
  //       path: "element-plus",
  //       component: () => import("@/views/table/element-plus/index.vue"),
  //       name: "ElementPlus",
  //       meta: {
  //         title: "Element Plus",
  //         keepAlive: true
  //       }
  //     },
  //     {
  //       path: "vxe-table",
  //       component: () => import("@/views/table/vxe-table/index.vue"),
  //       name: "VxeTable",
  //       meta: {
  //         title: "Vxe Table",
  //         keepAlive: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/menu",
  //   component: Layouts,
  //   redirect: "/menu/menu1",
  //   name: "Menu",
  //   meta: {
  //     title: "多级路由",
  //     svgIcon: "menu"
  //   },
  //   children: [
  //     {
  //       path: "menu1",
  //       component: () => import("@/views/menu/menu1/index.vue"),
  //       redirect: "/menu/menu1/menu1-1",
  //       name: "Menu1",
  //       meta: {
  //         title: "menu1"
  //       },
  //       children: [
  //         {
  //           path: "menu1-1",
  //           component: () => import("@/views/menu/menu1/menu1-1/index.vue"),
  //           name: "Menu1-1",
  //           meta: {
  //             title: "menu1-1",
  //             keepAlive: true
  //           }
  //         },
  //         {
  //           path: "menu1-2",
  //           component: () => import("@/views/menu/menu1/menu1-2/index.vue"),
  //           redirect: "/menu/menu1/menu1-2/menu1-2-1",
  //           name: "Menu1-2",
  //           meta: {
  //             title: "menu1-2"
  //           },
  //           children: [
  //             {
  //               path: "menu1-2-1",
  //               component: () => import("@/views/menu/menu1/menu1-2/menu1-2-1/index.vue"),
  //               name: "Menu1-2-1",
  //               meta: {
  //                 title: "menu1-2-1",
  //                 keepAlive: true
  //               }
  //             },
  //             {
  //               path: "menu1-2-2",
  //               component: () => import("@/views/menu/menu1/menu1-2/menu1-2-2/index.vue"),
  //               name: "Menu1-2-2",
  //               meta: {
  //                 title: "menu1-2-2",
  //                 keepAlive: true
  //               }
  //             }
  //           ]
  //         },
  //         {
  //           path: "menu1-3",
  //           component: () => import("@/views/menu/menu1/menu1-3/index.vue"),
  //           name: "Menu1-3",
  //           meta: {
  //             title: "menu1-3",
  //             keepAlive: true
  //           }
  //         }
  //       ]
  //     },
  //     {
  //       path: "menu2",
  //       component: () => import("@/views/menu/menu2/index.vue"),
  //       name: "Menu2",
  //       meta: {
  //         title: "menu2",
  //         keepAlive: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/hook-demo",
  //   component: Layouts,
  //   redirect: "/hook-demo/use-fetch-select",
  //   name: "HookDemo",
  //   meta: {
  //     title: "Hook",
  //     elIcon: "Menu",
  //     alwaysShow: true
  //   },
  //   children: [
  //     {
  //       path: "use-fetch-select",
  //       component: () => import("@/views/hook-demo/use-fetch-select.vue"),
  //       name: "UseFetchSelect",
  //       meta: {
  //         title: "useFetchSelect"
  //       }
  //     },
  //     {
  //       path: "use-fullscreen-loading",
  //       component: () => import("@/views/hook-demo/use-fullscreen-loading.vue"),
  //       name: "UseFullscreenLoading",
  //       meta: {
  //         title: "useFullscreenLoading"
  //       }
  //     },
  //     {
  //       path: "use-watermark",
  //       component: () => import("@/views/hook-demo/use-watermark.vue"),
  //       name: "UseWatermark",
  //       meta: {
  //         title: "useWatermark"
  //       }
  //     }
  //   ]
  // }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page",
    name: "Permission",
    meta: {
      title: "权限",
      svgIcon: "lock",
      roles: ["admin", "editor"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "page",
        component: () => import("@/views/permission/page.vue"),
        name: "PagePermission",
        meta: {
          title: "页面级",
          roles: ["admin"] // 或者在子导航中设置角色
        }
      },
      {
        path: "directive",
        component: () => import("@/views/permission/directive.vue"),
        name: "DirectivePermission",
        meta: {
          title: "按钮级" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  }
]

export const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}
